package com.ruoyi.web.controller.job;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.client.MovieClient;
import com.ruoyi.common.constant.MovieConstant;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.ScriptRecordInfo;
import com.ruoyi.system.domain.vo.movie.FilmPermitInfoVo;
import com.ruoyi.system.domain.vo.movie.RecordInfoDetailVo;
import com.ruoyi.system.domain.vo.movie.RecordInfoVo;
import com.ruoyi.system.service.impl.ScriptRecordInfoServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @ClassName ScriptRecordInfoTask
 * @Author YangFu
 * @Date 2024/3/23 17:32
 * @Version 1.0
 */
@Slf4j
@Component
@EnableScheduling
@RequiredArgsConstructor
public class ScriptRecordInfoTask {

    private final ScriptRecordInfoServiceImpl scriptRecordInfoService;

    private final MovieClient movieClient;

    private final RedisCache redisCache;

   // @Scheduled(cron = "0 0/2 * * * ?")
    //   @Async
   // @Scheduled(fixedRate = 10000) // 10秒
    public void getScriptRecordInfo() {

        Integer moviePage = redisCache.getCacheObject("record_page");
        int page;
        if (Objects.isNull(moviePage)) {
            redisCache.setCacheObject("record_page", 1);
            page = 1;
        } else {
            page = moviePage;
        }
        System.out.println("开始了===" + page);
        Map<String, Object> map = new HashMap<>();

        map.put("channelid", "256296");
//        map.put("badw", "重庆");
        map.put("page", "1");
        map.put("size", "9999");
        String url = MovieConstant.FILM_PERMIT_INFO_URL;
        R r = movieClient.postRequest1(url, map, RecordInfoVo.class);
        RecordInfoVo data = (RecordInfoVo) r.getData();
        if (Objects.isNull(data)) return;
        Integer total = data.getTotal();
        // page = page * 20 < total ? page + 1 : 1;
        if (page * 20 < total) {
            redisCache.setCacheObject("record_page", page + 1);
        } else {
            return;
        }
        System.out.println("数据=====" + JSON.toJSONString(data));
        List<RecordInfoDetailVo> detailVos = data.getData();
        List<ScriptRecordInfo> scriptRecordInfos = new ArrayList<>();
        for (RecordInfoDetailVo detailVo : detailVos) {
            String badi = detailVo.getBadi();
            //不包含重庆的不做处理
            if (!badi.contains("重庆"))continue;
            ScriptRecordInfo info = new ScriptRecordInfo();
            info.setAddress(badi);
            info.setRecordNum(detailVo.getBalx());
            info.setTitle(detailVo.getPmin());
            info.setOutcome(detailVo.getBajg());
            info.setUnit(detailVo.getBadw());
            info.setWriters(detailVo.getBiju());
            //TODO解析页面获取类型和概述
            String docpuburl = detailVo.getDocpuburl();
            info.setDocpubUrl(docpuburl);
            info.setNoticeTime(LocalDateTime.now());
            scriptRecordInfos.add(info);
        }
        scriptRecordInfoService.saveBatch(scriptRecordInfos);
    }
}
